package com.lzt.flow.entity;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

@Getter
@Setter
@ToString
public class Node {
    private String id;
    private String type;
    private String sourceNodeId;
    private String targetNodeId;
    private JSONObject properties;

    /**
     * 只维护到上一层
     */
    private List<Node> preNodeList;

    /**
     * 树结构，多层
     */
    private List<Node> nextNodeList;

    public String getNodeName() {
        return this.getType() + "_" + this.getId();
    }

    /**
     * 获取上一个节点
     *
     * @return
     */
    public Node getPreNode() {
        if (CollUtil.isEmpty(preNodeList)) {
            return null;
        }
        return getPreNodeList().get(0);
    }

    /**
     * 获取最后一个节点
     *
     * @return
     */
    public Node getLastNode() {
        if (getNextNodeList().size() == 0) {
            return null;
        }
        Node lastNode = null;
        Node nextNode = getNextNodeList().get(0);
        while (nextNode != null) {
            if (nextNode.getNextNodeList().size() == 0) {
                lastNode = nextNode;
                break;
            }
            nextNode = nextNode.getNextNodeList().get(0);
        }
        return lastNode;
    }
}
